<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Closure;
use app\common\models\AdminUser;
use app\common\exceptions\BizException;
use app\common\traits\ResponseJsonTrait;

class AuthenticateAdmin
{
    use ResponseJsonTrait;

    public function handle($request, Closure $next) {
        $user = \Auth::guard('admin')->user();

        if(!$user || !$user->uid){
            $this->relogin();
        }

        if ($user->status == AdminUser::STATUS_BAN) {
            throw new BizException('您已被禁用，请联系管理员');
        }
        $uid = $user->uid;
        if ($uid == 1) {
            \YunCms::$isFounder = true;
        }
        \YunCms::$adminUid = $uid;
        \YunCms::$adminUserName = $user->username;
        //request()->setRoute("\\App\\backend\\controllers\\TestController@test1");
        return $next($request);
    }
    private function relogin()
    {
        \Auth::guard('admin')->logout();
        request()->session()->flush();
        request()->session()->regenerate();

        //Utils::removeUniacid();

        return $this->errorJson('用户不存在，请重新登录', ['login_status' => 1, 'login_url' => '/admin/login/index']);

    }
}
